package webservice;



import common.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;


/**
 *
 * @author catmin
 */

@WebService(name="Autoverleih")
@SOAPBinding(style = Style.RPC)
public class AutoVerleih implements IAutoverleih{

	private java.util.Map<String, CarInfo> liste = new HashMap<String, CarInfo>();
	private CurrencyClient currencyClient = new CurrencyClient();

	public AutoVerleih() {
    	try {
    		currencyClient.connect();
		} catch (MalformedURLException e) {
			System.err.println("error init currency-client");
		}
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new FileReader("carlist.txt"));
                } catch (FileNotFoundException ex) {
                    System.out.println("carlist.txt not found!");
                    System.exit(-1);
                }

		CarInfo carInfo;
                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                        carInfo = new CarInfo();
                        String[] info = line.split("!");
                        carInfo.kennzeichen = info[0];
                        carInfo.typ = info[1];
                        carInfo.kosten = Float.parseFloat(info[2]);
                        carInfo.waehrung = info[3];
                        carInfo.verliehen = Boolean.parseBoolean(info[4]);
                        addCar(carInfo);
                    }
                } catch (IOException ex) {
                    System.out.println("carlist.txt -> I/O exception!");
                    System.exit(-1);
                }
	}

	private void addCar(CarInfo car){
		liste.put(car.kennzeichen, car);
	}

	@WebMethod(operationName="getCarInfo")
	@WebResult(name = "CarInfo")
	public CarInfo getCarInfo (
			@WebParam(name="car") Car car,
			@WebParam(name="currency") Currency cur) throws Exception{

		debug("getCarInfo von " + car.kennzeichen);
		// kz in db?
		if( ! liste.containsKey(car.kennzeichen)){
			error("kennzeichen " + car.kennzeichen + " not found");
			throw new Exception("not found");
		}
		// create response
		CarInfo car_in_liste = liste.get(car.kennzeichen);
		float kosten = currencyClient.calculate(car_in_liste.waehrung, cur.kuerzel, car_in_liste.kosten);
		CarInfo carInfo = new CarInfo();
                carInfo.typ = car_in_liste.typ;
		carInfo.kennzeichen = car_in_liste.kennzeichen;
		carInfo.verliehen = car_in_liste.verliehen;
		carInfo.waehrung = cur.kuerzel;
		carInfo.kosten = kosten;
		debug("sending response " + carInfo);
		return carInfo;
	}

	@WebMethod(operationName="getAllCars")
	@WebResult(name = "CarList")
	public CarInfo[] getAllCars(){
		debug("getAllCars");
		CarInfo[] list_to_send = new CarInfo[liste.size()];
		int i = 0;
		for(CarInfo c : liste.values()){
			list_to_send[i] = c;
			i++;
		}

		return list_to_send;
	}

	@WebMethod(operationName="rentCar")
	@WebResult(name = "RentResponse")
	public boolean rentCar(
			@WebParam(name = "car", partName = "car") Car car) throws Exception{
		if( ! liste.containsKey(car.kennzeichen)){
			error("kennzeichen " + car.kennzeichen + " not found");
			throw new Exception("not found");
		}
		// get car
		CarInfo car_in_liste = liste.get(car.kennzeichen);

		if(car_in_liste.verliehen){
			return false;
		}

		// verleih durchfuehern
		car_in_liste.verliehen = true;
		return true;
	}

	@WebMethod(operationName="returnCar")
	@WebResult(name = "ReturnResponse")
	public boolean returnCar(
			@WebParam(name = "car", partName = "car") Car car) throws Exception{
		if( ! liste.containsKey(car.kennzeichen)){
			error("kennzeichen " + car.kennzeichen + " not found");
			throw new Exception("not found");
		}
		// get car
		CarInfo car_in_liste = liste.get(car.kennzeichen);

		// muss verliehen sein
		if( ! car_in_liste.verliehen){
			return false;
		}

		// rueckgabe durchfuehern
		car_in_liste.verliehen = false;

		return true;
	}



	public static void debug(String message){

	}

	public static void error(String message){

	}
}